#include <iostream>
#include <fstream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <chrono>

#define SERVER_IP "127.0.0.1"
#define PORT 9999
#define BUFFER_SIZE 1024

int main()
{
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
    {
        perror("socket");
        return 1;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    inet_pton(AF_INET, SERVER_IP, &addr.sin_addr);

    std::ifstream file("large_file.bin", std::ios::binary);
    auto start = std::chrono::high_resolution_clock::now();

    char buffer[BUFFER_SIZE];
    while (!file.eof())
    {
        file.read(buffer, BUFFER_SIZE);
        ssize_t bytes_read = file.gcount();
        sendto(sock, buffer, bytes_read, 0, (struct sockaddr *)&addr, sizeof(addr));
    }

    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> duration = end - start;
    std::cout << "UDP 传输耗时: " << duration.count() << " 秒" << std::endl;

    file.close();
    close(sock);
    return 0;
}